package com.scau.hwadee.examination.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scau.hwadee.examination.entity.Major;
import com.scau.hwadee.examination.entity.Majorclass;
import com.scau.hwadee.examination.entity.Nationwidemajor;
import com.scau.hwadee.examination.service.ProfessionalManagementService;
import com.scau.hwadee.examination.utils.ExportExcelUtils;
import com.scau.hwadee.examination.utils.ImportExcelUtils;
import com.scau.hwadee.examination.utils.JsonUtils;
import com.scau.hwadee.examination.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author: YHX
 * @date: 2019/7/28 20:12
 * @description:
 */
@Slf4j
@Controller
@RequestMapping("professional/management")
@Api(tags = "ProfessionalManagementController", description = "专业管理模块")
public class ProfessionalManagementController {

    @Autowired
    private ProfessionalManagementService professionalManagementService;

    @GetMapping("allMajorClasses")
    @ApiOperation(value = "显示专业大类的所有信息")
    public ResponseEntity<ResponseVo> getAllMajorClasses(@RequestParam("pageNum") int pageNum,
                                                         @RequestParam("pageSize") int pageSize) {
        log.debug("显示专业大类的所有信息");
        ResponseVo vo = ResponseVo.instance();
        vo.setData(professionalManagementService.getAllMajorClass(pageNum, pageSize));
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @PutMapping("major")
    @ApiOperation(value = "修改专业信息")
    public ResponseEntity<ResponseVo> updateMajorMessage(@RequestBody String param) {
        ResponseVo vo = ResponseVo.instance();
        log.debug("修改专业信息");
        Major major = JsonUtils.JSONToObject(param, Major.class);
        int cnt = professionalManagementService.updateMajorMessage(major);
        String message;
        if (cnt > 0) {
            message = "修改专业信息成功";
        } else {
            message = "修改专业信息失败";
        }
        vo.setMessage(message);
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @PostMapping("newMajorClass")
    @ApiOperation(value = "增加新的专业大类")
    @ApiImplicitParam(name = "majorclass", value = "专业大类信息", dataType = "Majorclass", required = true)
    public ResponseEntity<ResponseVo> createMajorClass(@RequestBody String data) {
        log.debug("增加专业大类");
        Majorclass majorclass = JsonUtils.JSONToObject(data, Majorclass.class);
        ResponseVo vo = ResponseVo.instance();
        int cnt = professionalManagementService.createNewMajorClass(majorclass);
        String message;
        if (cnt > 0) {
            message = "增加专业大类成功";
        } else {
            message = "增加专业大类失败";
        }
        vo.setMessage(message);
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @PutMapping("majorClass")
    @ApiOperation(value = "修改专业大类的信息")
    @ApiImplicitParam(name = "majorclass", value = "专业大类信息", dataType = "Majorclass", required = true)
    public ResponseEntity<ResponseVo> updateMajorClassByPrimaryKey(@RequestBody Map<String, String> params) {
        log.debug("修改专业大类信息");
        ResponseVo vo = ResponseVo.instance();
        String beforeName = params.get("beforeName");
        String afterId = params.get("afterId");
        String afterName = params.get("afterName");
        int cnt = professionalManagementService.updateMajorClass(beforeName, afterId, afterName);
        String message;
        if (cnt > 0) {
            message = "更新专业大类信息成功";
        } else {
            message = "更新专业大类信息失败";
        }
        vo.setMessage(message);
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping("allMajor")
    @ApiOperation(value = "获取所有的专业")
    public ResponseEntity<ResponseVo> getAllMajor(@RequestParam("pageNum") int pageNum,
                                                  @RequestParam("pageSize") int pageSize) {
        log.debug("获取所有的专业");
        ResponseVo vo = ResponseVo.instance();
        vo.setData(professionalManagementService.getAllMajor(pageNum, pageSize));
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping("subjectSchool")
    @ApiOperation(value = "根据主键获取主考院校")
    public ResponseEntity<ResponseVo> getSubjectSchoolByPrimaryKey(@RequestParam("schoolId") String schoolId) {
        ResponseVo vo = ResponseVo.instance();
        vo.setData(professionalManagementService.getSubjectSchool(schoolId));
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping("allSubjectSchools")
    @ApiOperation(value = "获取所有的专业主考院校")
    public ResponseEntity<ResponseVo> getAllSubjectSchools(@RequestParam("pageNum") int pageNum,
                                                           @RequestParam("pageSize") int pageSize) {
        log.debug("获取所有的专业主考院校");
        ResponseVo vo = ResponseVo.instance();
        vo.setData(professionalManagementService.getAllMajorSubjectSchools(pageNum, pageSize));
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @PostMapping("subjectSchool")
    @ApiOperation(value = "设置专业主考院校")
    public ResponseEntity<ResponseVo> setSubjectSchool(@RequestBody String data) {
        log.debug("设置专业的主考院校");
        ResponseVo vo = ResponseVo.instance();
        JSONArray params = Objects.requireNonNull(JsonUtils.JSONToObject(data, JSONObject.class)).getJSONArray("jsonArray");
        JSONObject json = params.getJSONObject(0);
        String first = json.get("schoolId").toString();
        String majorId = json.get("majorId").toString();
        int cnt = professionalManagementService.updateSubjectSchoolOfMajorByMajorId(first, majorId);
        StringBuffer message = new StringBuffer();
        if (cnt > 0) {
            message.append("设置专业的第一主考院校成功\n");
        } else {
            message.append("设置专业的第一主考院校失败\n");
        }
        cnt = professionalManagementService.insertExaminerList(params);
        if (cnt > 0) {
            message.append("设置专业主考院校成功\n");
        } else {
            message.append("设置专业主考院校失败\n");
        }
        vo.setMessage(message.toString());
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @PostMapping("newMajor")
    @ApiOperation(value = "添加专业")
    @ApiImplicitParam(name = "major", value = "专业信息", dataType = "major", required = true)
    public ResponseEntity<ResponseVo> createNewMajor(@RequestBody String param) {
        Major major = JsonUtils.JSONToObject(param, Major.class);
        log.debug("转的json字符串为 " + param);
        log.debug("添加专业");
        ResponseVo vo = ResponseVo.instance();
        int cnt = professionalManagementService.createNewMajor(major);
        String message;
        if (cnt > 0) {
            message = "添加专业成功";
        } else {
            message = "添加专业失败";
        }
        vo.setMessage(message);
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @PostMapping("importNationalMajorExcel")
    @ApiOperation(value = "导入全国专业的Excel信息表")
    public ResponseEntity<ResponseVo> importNationalMajorExcel(MultipartFile file) {
        log.debug("导入全国专业信息的Excel表");
        ResponseVo vo = ResponseVo.instance();
        try {
            List<Nationwidemajor> excelMessage = ImportExcelUtils.parseNationalMajorExcel(file.getInputStream());
            int totalInsert = professionalManagementService.insertNationalMajorFromExcel(excelMessage);
            String message = "导入的专业总数为：" + totalInsert;
            vo.setMessage(message);
            return new ResponseEntity<>(vo, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            vo.setMessage("导入专业信息表出错");
            return new ResponseEntity<>(vo, HttpStatus.OK);
        }
    }

    @PostMapping("importExcel")
    @ApiOperation(value = "导入专业的Excel信息表")
    public ResponseEntity<ResponseVo> importExcel(MultipartFile file) {
        log.debug("导入专业信息的Excel表");
        ResponseVo vo = ResponseVo.instance();
        try {
            List<Major> excelMessage = ImportExcelUtils.parseMajorExcel(file.getInputStream());
//            for (int i = 0; i < excelMessage.size(); i++) {
//                System.out.println(excelMessage.get(i));
//            }
            int totalInsert = professionalManagementService.insertMajorFromExcel(excelMessage);
            String message = "导入的专业总数为：" + totalInsert;
            vo.setMessage(message);
            return new ResponseEntity<>(vo, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            vo.setMessage("导入专业信息表出错");
            return new ResponseEntity<>(vo, HttpStatus.OK);
        }
    }

    @GetMapping("allNationalwideMajor2ExportExcel")
    @ApiOperation(value = "导出全国专业信息表")
    public ResponseEntity<byte[]> exportNationalwideMajor2Excel() {
        log.debug("导出全国专业信息为Excel表");
        List<Nationwidemajor> nationwidemajors = professionalManagementService.getAllNationalMajor2ExportExcel();
        return ExportExcelUtils.exportNationwideMajor2Excel(nationwidemajors);
    }

    @GetMapping("allSCProvinceMajor2ExportExcel")
    @ApiOperation(value = "导出四川省专业信息表")
    public ResponseEntity<byte[]> exportSCProvinceMajor2Excel() {
        log.debug("导出四川省专业信息为Excel表");
        List<Major> majors = professionalManagementService.getAllMajor2ExportExcel();
        return ExportExcelUtils.exportSCProvinceMajor2Excel(majors);
    }

    @PutMapping("majorState")
    @ApiOperation(value = "专业的启用和停用")
    @ApiImplicitParam(name = "params", value = "需要启用护着停用的专业列表", dataType = "JSONArray", required = true)
    public ResponseEntity<ResponseVo> changeMajorState(@RequestBody String data) {
        log.debug("修改专业的状态为启用或者停用");
        JSONObject json = JsonUtils.JSONToObject(data, JSONObject.class);
        JSONArray params = Objects.requireNonNull(json).getJSONArray("jsonArray");
        ResponseVo vo = ResponseVo.instance();
        List<Major> majors = new ArrayList<>();
        for (int i = 0; i < params.size(); i++) {
            JSONObject map = params.getJSONObject(i);
            String majorId = map.get("majorId").toString();
            String state = map.get("state").toString();
            Major major = new Major();
            major.setMjId(majorId);
            major.setMjState(state);
            majors.add(major);
        }
        int cnt = professionalManagementService.updateMajorEnableOrDisable(majors);
        String message;
        message = "修改成功的专业为：" + cnt;
        vo.setMessage(message);
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping("allNationalwideMajor")
    @ApiOperation(value = "获取全国专业列表")
    public ResponseEntity<ResponseVo> getAllNationalwideMajor(@RequestParam("pageNum") int pageNum,
                                                              @RequestParam("pageSize") int pageSize) {
        log.debug("获取全国专业列表信息");
        ResponseVo vo = ResponseVo.instance();
        vo.setData(professionalManagementService.getAllNationalMajor(pageNum, pageSize));
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @PostMapping("newNationalwideMajor")
    @ApiOperation(value = "新增全国专业")
    public ResponseEntity<ResponseVo> createNewNationalwideMajor(@RequestBody Nationwidemajor nationwidemajor) {
        log.debug("新增全国专业");
        ResponseVo vo = ResponseVo.instance();
        int cnt = professionalManagementService.createNewNationalMajor(nationwidemajor);
        String message;
        if (cnt > 0) {
            message = "新增全国专业成功";
        } else {
            message = "新增全国专业成功";
        }
        vo.setMessage(message);
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @DeleteMapping(value = "NationalwideMajor", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "删除全国专业")
    @ApiImplicitParam(name = "params", value = "全国专业主键json数组", dataType = "JSONArray", required = true)
    public ResponseEntity<ResponseVo> deleteNewNationalwideMajor(@RequestBody String data) {
        ResponseVo vo = ResponseVo.instance();
        log.debug("删除全国专业");
        log.debug(data);
        JSONObject jsonObject = JsonUtils.JSONToObject(data, JSONObject.class);
        JSONArray params = Objects.requireNonNull(jsonObject).getJSONArray("jsonArray");
        int cnt = professionalManagementService.deleteNationalMajorList(params);
        String message;
        if (cnt > 0) {
            message = "删除全国专业成功";
        } else {
            message = "删除全国专业失败";
        }
        vo.setMessage(message);
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }
}
